package com.ztesoft.web.taskgenerator.db.dao;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.common.Configuration;
import com.ztesoft.web.taskgenerator.db.arg.DmTaskArg;
import com.ztesoft.web.taskgenerator.db.arg.DmTaskArg.DmTaskCriteria;
import com.ztesoft.web.taskgenerator.db.dao.mapper.IDmTaskMapper;
import com.ztesoft.web.taskgenerator.db.po.DmTaskPO;

@Repository
public class DmTaskDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(DmTaskArg arg) {
		return getMapper().countByArg(arg);
	}

	public int deleteByArg(DmTaskArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public DmTaskPO changeTime(Integer dmPlanId) {
		return getMapper().changeTime(dmPlanId);
	}

	public List<DmTaskPO> selectByArg(DmTaskArg arg) {
		return getMapper().selectByArg(arg);
	}
	
	public List<DmTaskPO> selectByParentTaskId(Integer dmPlanId) {
		return getMapper().selectByParentTaskId(dmPlanId);
	}

	/**
	 * 获取计划下的最新任务信息
	 * 
	 * @param dmPlanId
	 * @return
	 */
	public DmTaskPO getOneBydmPlanId(Integer dmPlanId) {
		return getMapper().getOneBydmPlanId(dmPlanId);
	}

	/**
	 * 判断任务是否已经执行过了
	 */
	public DmTaskPO isStarted(DmTaskPO dtPO) {
		return getMapper().isStarted(dtPO);
	}

	/**
	 * 判断任务结果明细表是否生成
	 */	
	public DmTaskPO hasCreated(DmTaskPO dtPO) {
		return getMapper().hasCreated(dtPO);
	}

	/**
	 * 更新任务结果恢复明细记录状态
	 * @param record
	 * @param arg
	 * @return
	 */
	public int updateRD(String tableName,String stateDate,String dmDetailId) {
		return getMapper().updateRD(tableName, stateDate, dmDetailId);
	}
	
	/**
	 * 更新任务结果恢复明细记录状态--失败
	 * @param record
	 * @param arg
	 * @return
	 */
	public int updateRDError(String tableName,String stateDate,String dmDetailId) {
		return getMapper().updateRDError(tableName, stateDate, dmDetailId);
	}
	
	public int updateByArgSelective(DmTaskPO record, DmTaskArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}

	public int updateByArg(DmTaskPO record, DmTaskArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<DmTaskPO> selectByArgAndPage(DmTaskArg arg, Page<DmTaskPO> resultPage) {
		List<DmTaskPO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	/**
	 * 数据恢复界面
	 * 
	 * @param arg
	 * @param resultPage
	 * @return
	 */
	public Page<DmTaskPO> queryRecordByPageForDataRecovery(DmTaskPO record, Page<DmTaskPO> resultPage) {
		List<DmTaskPO> resultList = getMapper().queryRecordByPageForDataRecovery(record, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(DmTaskPO record) {
		return getMapper().insert(record);
	}

	public int insertSelective(DmTaskPO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<DmTaskPO> records) {
		return getMapper().insertBatch(records);
	}

	public int deleteByPrimaryKey(Integer key) {
		return getMapper().deleteByPrimaryKey(key);
	}

	// 删除DmTask表里状态为00T的记录
	public int delete00T(Integer key) {
		return getMapper().delete00T(key);
	}

	public DmTaskPO selectByPrimaryKey(Integer key) {
		return getMapper().selectByPrimaryKey(key);
	}

	public int updateByPrimaryKeySelective(DmTaskPO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(DmTaskPO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询mysql指定库是否已存在——表
	 * 
	 * @param tableName
	 * @return
	 */
	public int selectExistTableName(String tableName) {
		return getMapper().selectExistTableName(tableName, Configuration.getString("TABLE_SCHEMA"));
	}

	/**
	 * 创建任务结果恢复明细表
	 * @param tableName
	 * @return
	 */
	public int addCreateRDmTable(String tableName) {
		return getMapper().addCreateRDmTable(tableName);
	}
	
	/**
	 * 将任务结果明细表的数据移动到任务结果恢复明细表
	 * @param tableName
	 * @return
	 */
	public int addRDmTableDate(String tableName) {
		return getMapper().addRDmTableDate(tableName);
	}
	
	public int addCreateDmTable(String tableName, String sourceTableName) {
		return getMapper().addCreateDmTable(tableName, sourceTableName);
	}

	public List<DmTaskPO> selectByNeedSubtask() {
		return getMapper().selectByNeedSubtask();
	}

	public int updateByNeedSubTask(Integer dmTaskId) {
		return getMapper().updateByNeedSubTask(dmTaskId);
	}

	public List<DmTaskPO> getBusDmTaskPO() {
		return getMapper().getBusDmTaskPO();
	}

	public List<DmTaskPO> getNotBusDmTaskPO() {
		return getMapper().getNotBusDmTaskPO();
	}

	public int updateTaskById(String state, Integer dmTaskId) {
		return getMapper().updateTaskById(state, dmTaskId);
	}

	public int updateTaskById_(Integer isCheck, Integer dmTaskId) {
		return getMapper().updateTaskById_(isCheck, dmTaskId);
	}

	/**
	 * 根据计划id查询所有状态为失败和暂停的任务
	 * 
	 * @param dmPlanId
	 * @return
	 */
	public List<DmTaskPO> selectTaskPOByPlanIdStart(Integer dmPlanId) {
		return getMapper().selectTaskPOByPlanIdStart(dmPlanId);
	}

	public List<DmTaskPO> selectTaskPOByPlanIdPause(Integer dmPlanId) {
		return getMapper().selectTaskPOByPlanIdPause(dmPlanId);
	}

	public List<DmTaskPO> selectTaskPOByPlanIdCancle(Integer dmPlanId) {
		return getMapper().selectTaskPOByPlanIdCancle(dmPlanId);
	}
	/**
	 *  查询手工任务
	 * @param dmPlanId
	 * @param dmTaskId
	 * @return
	 */
	public List<DmTaskPO> selectTaskForCreateHandTask(Integer priority, Integer dmPlanId, Integer dmTaskId) {
		return getMapper().selectTaskForCreateHandTask(priority, dmPlanId, dmTaskId);
	}

	public List<DmTaskPO> selectEditTask(Integer priority, Integer dmPlanId) {
		return getMapper().selectEditTask(priority, dmPlanId);
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<DmTaskPO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	public List<DmTaskPO> selectByDmTaskInfo(Integer dmPlanId, Integer groupNbr) {
		return getMapper().selectByDmTaskInfo(dmPlanId, groupNbr);
	}

	public List<DmTaskPO> selectByTaskInfoNotState(Integer dmPlanId, Integer groupNbr) {
		return getMapper().selectByTaskInfoNotState(dmPlanId, groupNbr);
	}

	private DmTaskArg buildQueryObject(Map<String, Object> params) {

		DmTaskArg arg = new DmTaskArg();
		DmTaskCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public IDmTaskMapper getMapper() {
		return getSqlSession().getMapper(IDmTaskMapper.class);
	}

	// 查询该计划是否满足删除条件
	public List<DmTaskPO> selectBydmPlanId(Integer dmPlanId) {
		return getMapper().selectBydmPlanId(dmPlanId);
	}

	public int updateTask_(int isCheck, int[] taskIdArr) {
		//
		return getMapper().updateTask_(isCheck, taskIdArr);

	}
}
